package com.dycong.common.common;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateFormatUtils {
	/**
	 *TODO 日期格式是很灵活的,随便怎么定义,只要'y','M','d','H','m','d','S'这些字母就可以,例如:"ss/m/H/d@M ? yy"
	 * yyyy-MM-dd
	 */
	public final static String V_SHORT_DATE_HOUR = "yyyy-MM-dd HH";
	public final static String V_SHORT_DATE = "yyyy-MM-dd";
	public final static String SHORT_DATE = "yyyyMMdd";
	public final static String O_SHORT_DATE = "MM/dd/yyyy";
	public final static String V_LONG_DATE = "yyyy-MM-dd HH:mm:ss";
	public final static String O_LONG_DATE = "MM/dd/yyyy HH:mm:ss";
	public final static String V_LONG_DATE_1 = "yyyyMMddHHmmss";
	public final static String V_LONG_DATE_2 = "yyMMddhhmmss";
	public final static String V_LONG_DATE_3 = "yyyyMMddhhmmss";
	public final static String V_LONG_YMD = "yyyyMMdd";
	public final static String V_SHORT_YM = "yyyyMM";
	public final static String V_SHORT_DATE2 = "yyyy年MM月dd";
	public final static String MMDD = "MMdd";
	public final static String CRON = "ss mm HH dd MM ? yyyy";
//	public final static String CRON = "ss/m/H/d@M ? yy";

	/**
	 * 获取2个时间之间的天数差
	 * 
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(Date smallDate, Date bigDate) {
		if (smallDate == null || bigDate == null)
			return 0;
		long s = bigDate.getTime() - smallDate.getTime();
		BigDecimal big = new BigDecimal(s);
		BigDecimal base = new BigDecimal(3600 * 24 * 1000);
		BigDecimal res = big.divide(base, BigDecimal.ROUND_UP,
				BigDecimal.ROUND_DOWN);
		return res.intValue();
	}
	
	/**
	 * 获取2个时间之间的小時差
	 * 
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 */
	public static long hoursBetween(Date smallDate, Date bigDate) {
		if (smallDate == null || bigDate == null)
			return 0;
		long s = bigDate.getTime() - smallDate.getTime();
		BigDecimal big = new BigDecimal(s);
		BigDecimal base = new BigDecimal(60*60 * 1000);
		/*BigDecimal.ROUND_UP:计算得到的商小数位个数*/
		BigDecimal res = big.divide(base, BigDecimal.ROUND_UP,
				BigDecimal.ROUND_DOWN);
		return res.longValue();
	}

	public static Date formatStringToLongDate(String value, String dateType)
			throws ParseException {
		Date date = null;
		SimpleDateFormat format = new SimpleDateFormat(dateType);
		date = format.parse(value);
		return date;
	}

	public static String formatDateToString(Date date, String dateType) {
		SimpleDateFormat format = new SimpleDateFormat(dateType);
		return format.format(date);
	}

	public static String formatDateStrToStr(String value, String srcDateType,
			String destDateType) throws ParseException {
		Date date = DateFormatUtils.formatStringToLongDate(value, srcDateType);
		String result = DateFormatUtils.formatDateToString(date, destDateType);
		return result;
	}

	public static String getLastMonth(String dateStr, String dateType)
			throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(dateType);
		Date date = formatter.parse(dateStr);
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -1);

		return formatter.format(calendar.getTime());
	}

	public static String getLastDate(String dateStr, String dateType)
			throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(dateType);
		Date date = formatter.parse(dateStr);
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);

		return formatter.format(calendar.getTime());
	}

	public static ArrayList<String> getTransDateTimeRange(String transadate)
			throws ParseException {
		ArrayList<String> transDateTimelist = new ArrayList<String>();
		String transDateTimeEnd = transadate + " 23:00:00";

		SimpleDateFormat formatter = new SimpleDateFormat(V_LONG_DATE);
		Date date = formatter.parse(transDateTimeEnd);
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);

		String transDateTimeBegin = formatter.format(calendar.getTime());

		transDateTimelist.add(transDateTimeBegin);
		transDateTimelist.add(transDateTimeEnd);
		return transDateTimelist;
	}

	public static Date formatStringToDate(String date, String dateType)
			throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(dateType);
		return format.parse(date);
	}

	/**
	 * 传入时间 vs 当前时间 1：大于 0： 相等 -1：小于
	 */
	public static int dateCompare(String date, String format)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(date));
		return cal.getTime().compareTo(new Date());
	}

	/**
	 * 当前时间-参数时间=差
	 */
	public static Long currentDatePoor(Date date) {
		SimpleDateFormat simpleFormat = new SimpleDateFormat(V_LONG_DATE);
		simpleFormat.format(date);
		Date now = new Date();
		simpleFormat.format(now);
		return now.getTime() - date.getTime();
	}

	public static Date dateCompute(Date date, int i, int type) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(type, i);

		return calendar.getTime();
	}
	
	public static String getCron(Date date) {
		return formatDateToString(date, CRON);
	}
	
	public static String getPreviousDayStr(String dateType){
		SimpleDateFormat sdf = new SimpleDateFormat(dateType);
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, -1);
		
		return sdf.format(calendar.getTime());
	}
	
	
	public static void main(String[] args) throws ParseException {
		Date date=new Date();
		Date date2=dateCompute(date, 4, Calendar.HOUR);
		System.out.println(date);
		System.out.println(date2);
		System.out.println(hoursBetween(date, date2));
		System.out.println(getCron(date));
	}

}
